package com.cn.wanxi.servlet.product;

import com.alibaba.fastjson.JSONObject;
import com.cn.wanxi.model.ProductModel;
import com.cn.wanxi.model.ResultModel;
import com.cn.wanxi.service.ProductService;
import com.cn.wanxi.service.impl.ProductServiceImpl;
import com.cn.wanxi.util.Tool;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/back/product/add")
public class ProductAddServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        1.乱码处理
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
//        2.拿到前端数据
        String price = req.getParameter("price");
        String normalPrice = req.getParameter("normalPrice");
//        String imgHref = (String) req.getSession().getAttribute("imgHref");
        String imgHref=req.getParameter("imgHref");
        String content = req.getParameter("content");
        String cuisineId = req.getParameter("cuisineId");
        String name = req.getParameter("name");
//        3.封装model
        ProductModel model = new ProductModel();
        model.setCuisineId(Tool.strToInt(cuisineId));
        model.setContent(content);
//        将字符串转换为BigDecimal
        model.setNormalPrice(Tool.strToBigDecimal(normalPrice));
        model.setPrice(Tool.strToBigDecimal(price));
        model.setImgHref(imgHref);
        model.setName(name);
//        4。调用service
        ProductService productService = new ProductServiceImpl();
//        5.得到结果
        ResultModel resultModel = productService.add(model);
//        6.返回前端
        resp.getWriter().println(JSONObject.toJSONString(resultModel));
    }
}
